package com.ovopark.firecar.presenter

import android.content.Context
import android.util.Log
import android.widget.Toast
import com.alibaba.fastjson.JSON
import com.caoustc.okhttplib.okhttp.OkHttpRequestParams
import com.caoustc.okhttplib.okhttp.callback.StringHttpRequestCallback
import com.ovopark.firecar.bean.ArrivePeopleInforBean
import com.ovopark.firecar.bean.UserBean
import com.ovopark.firecar.manager.DataManager
import com.ovopark.firecar.manager.UserCacheManager
import com.ovopark.firecar.okhttp.OkHttpRequest
import com.ovopark.firecar.rsp.GetArriveAddressRsp
import com.ovopark.firecar.view.IReadcardView
import org.json.JSONObject
import java.io.File

class ReadcardPresenter(var mContext : Context,var view : IReadcardView) {

    fun getArriveAddress(){
        var param = OkHttpRequestParams()
        param.addBodyParameter("model","regional.information")
        param.addBodyParameter("function_name","api_page")
        param.addBodyParameter("access_token",UserCacheManager.getInstance()?.getUsers()?.access_token)
        param.addBodyParameter("uid",UserCacheManager.getInstance()?.getUsers()?.uid)
        OkHttpRequest.post(DataManager.getInstance().getBaseUrl()+DataManager.GET_ARRIVE_PLACE,param,object : StringHttpRequestCallback(){
            override fun onSuccess(data: String?) {
                super.onSuccess(data)
                var msg = JSONObject(data).optString("message")
                var code = JSONObject(data).optInt("code")
                if (code==0){
                    var result = JSON.parseObject(data,GetArriveAddressRsp::class.java)
                    view.getRecordSuccess(result.data)
                }else{
                    view.getRecordFailed(msg)
                }
            }

            override fun onFailure(errorCode: Int, msg: String?) {
                super.onFailure(errorCode, msg)
                view.getRecordFailed(msg)

            }
        })
    }

    fun addArriveInfor(arrivePeopleInforBean: ArrivePeopleInforBean
    ){
        var param = OkHttpRequestParams()
        param.addBodyParameter("model","epidemic.people.record")
        param.addBodyParameter("function_name","record_information")
        param.addBodyParameter("status","")
        param.addBodyParameter("phone_number","")
        param.addBodyParameter("state_id","")
        param.addBodyParameter("city_id","")
        param.addBodyParameter("area_id","")
        param.addBodyParameter("specific_address","")
        param.addBodyParameter("sex","")
        param.addBodyParameter("nation","")
        param.addBodyParameter("name",arrivePeopleInforBean.name)
        param.addBodyParameter("identification",arrivePeopleInforBean.id)
        param.addBodyParameter("with_shunt","")
        param.addBodyParameter("isolation_point",arrivePeopleInforBean.addressId)
        param.addBodyParameter("toge_number",arrivePeopleInforBean.toge_number)
        param.addBodyParameter("uid",UserCacheManager.getInstance()?.getUsers()?.uid)
        param.addBodyParameter("access_token",UserCacheManager.getInstance()?.getUsers()?.access_token)
        OkHttpRequest.post(DataManager.getInstance().getBaseUrl()+DataManager.ADD_ARRIVE_RECORD,param,object : StringHttpRequestCallback(){
            override fun onSuccess(data: String?) {
                super.onSuccess(data)
                var code = JSONObject(data).optInt("errcode")
                val mes = JSONObject(data).optString("message")

                if (code==0){
                    var result = JSONObject(data).optString("data")
                    var name = JSONObject(result).optString("name")
                    view.addArriveSuccess("${name} : ${mes}")
                }else{
                    view.addArriveFailed(mes)

                }
            }

            override fun onFailure(errorCode: Int, msg: String?) {
                super.onFailure(errorCode, msg)
                view.addArriveFailed(msg)

            }
        })

    }

}